<?php
class v1_manage{
    public function index(){
    	$session=$this->yum->library('session');
    	if($session->ISSESS()){
    		$role=$session->GVAR('role');
    		$purview=$session->GVAR('purview');
			if((count($purview)==1)&&(count($purview['manage'])==2)){
				$message=$this->yum->controller('message');
				$message->index(5,domain.'index.php?c=manage&a=out','no','您没有任何权限,请联系管理员,5秒后自动退出.');
				exit();
			}else if(!empty($role)){
    		  header('Location: '.domain.'index.php?c=account&a=info');
    		  exit();
    		}
    	}
    	$this->yum->view('index.php');
    }
    public function login(){
		$captcha=$this->yum->library('captcha');
		if(!$captcha->check($this->yum->post('captcha',true),600)){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','您输入的验证码有误.');
			exit();
		}
		$mail=strtolower($this->yum->post('mail',true));
		$password=$this->yum->post('password',true);
		$helper=$this->yum->library('helper');
		if(!$helper->CMail($mail)){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','请输入一个正确的邮箱.');
			exit();
		}
		$member=$this->yum->model('member');
		$user=$member->GMember(array('where'=>array('mail ='=>$mail)));
		$user=reset($user);
		if(empty($user)){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','这个帐户不存在,请确认后再登陆.');
			exit();
		}else if($user['password']!==$password){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','帐户与密码不匹配.');
			exit();
		}else if($user['locked']==1||$user['role']==2){
			$message=$this->yum->controller('message');
			$message->index(5,HTTP_REFERER,'no','帐户未开通访问权限,请联系管理员.');
			exit();
		}else{
			$role=$this->yum->model('role');
			$temp=$role->GRole(array('where'=>array('id ='=>$user['role'],'locked ='=>0)));
			if(empty($temp)){
				$message=$this->yum->controller('message');
				$message->index(5,HTTP_REFERER,'no','您所在的角色已被限制登录,请联系管理员.');
				exit();
			}
			$member->UMember(array('set'=>array('visits=visits+'=>1,'ip ='=>ip2long($_SERVER["REMOTE_ADDR"]),'date ='=>$this->yum->runtime),'where'=>array('mail ='=>$mail,'password ='=>$password)));
			$mation=$this->yum->model('information');
			$info=$mation->GMation(array('where'=>array('member ='=>$user['id'])));
			$info=reset($info);
			$session=$this->yum->library('session');
			$session->SVAR('member',$user['id']);
			$session->SVAR('mail',$mail);
			$session->SVAR('name',$info['name']);
			$session->SVAR('money',$info['money']);
			$session->SVAR('frozen',$info['frozen']);
			$session->SVAR('role',$user['role']);
			$session->SVAR('visits',$user['visits']);
			$session->SVAR('ip',$user['ip']);
			$session->SVAR('date',$user['date']);
			header('Location: '.domain.'index.php?c=account&a=info');
		}
    }
    public function captcha(){
    	$captcha=$this->yum->library('captcha');
		$captcha->showImage();
    }
    public function out(){
    	$session=$this->yum->library('session');
    	$session->destroy();
    	$message=$this->yum->controller('message');
		$message->index(5,domain,'yes','安全退出成功,5秒后转到登陆页.');
    }
}
?>